const app = getApp();

// const BASE_URL = 'https://test2.zhimeijia.top/';

let unLoginUrl = [
  'pages/index/index',
  'pages/classification/classification',
  'pages/home/pages/my/product-details/product-details',
]

const BASE_URL = 'https://zhim.zhimeijia.top/';
const http = ({
  url = '',
  param = {},
  noload ='',
  noToken=false,
  ...other
} = {}) => {

  if (!noload) {
    wx.showLoading({
      title: '请求中..'
    });
  }

  let header = {};
  header['Content-Type'] = 'application/json';
  let timeStart = Date.now();
  console.log(noToken)
  if(!noToken) {
    param.token = wx.getStorageSync('token') ? wx.getStorageSync('token') : ''

  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: getUrl(url),
      data: param,
      header: header,
      ...other,
      complete: (res) => {
        if (res.data.status) {
          if(!noload) {
            wx.hideLoading();
          }
          resolve(res.data);

        } else {
          if (res.data.data == 11027) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
            wx.setStorage({
              key: 'token',
              data: res.data.token,
            })
            // 在登录页弹框授权
            // setTimeout(function () {
            //   wx.reLaunch({
            //     url: '/pages/mine/pages/my/change-phoneNumber/change-phoneNumber',
            //   })
            // }, 1000)
          } else if (res.data.data == 14007 || res.data.data == 14006) {
            let pages = getCurrentPages()    //获取加载的页面
            let currentPage = pages[pages.length-1]    //获取当前页面的对象
            let url = currentPage.route 
            if(unLoginUrl.indexOf(url) == -1){
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/login/login',
                })
              }, 1000)
            } 
           
          }
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          if(!noload) {
            wx.hideLoading();
          }
          reject(res);
        }
      },
      fail(res) {
        if(!noload) {
          wx.hideLoading();
        }
        if(res.data == '10059'){
          reject(res)
          return
        }
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  })
}

const getUrl = (url) => {
  if (url.indexOf('://') == -1) {
    url = BASE_URL + url;
  }
  return url
}

const _get = (url, param = {},noload) => {
  return http({
    url,
    param,
    noload
  })
}

const _post = (url, param = {},noload,noToken) => {
  return http({
    url,
    param,
    method: 'post',
    noload,
    noToken
  })
}

const _put = (url, param = {}) => {
  return http({
    url,
    param,
    method: 'put'
  })
}

const _delete = (url, param = {}) => {
  return http({
    url,
    param,
    method: 'put'
  })
}



module.exports = {
  BASE_URL,
  _get,
  _post,
  _put,
  _delete
}